<template>
	<view>
		<view class="search_flex" @click="$pageGo('/pages/index/search')">
			<uni-icons color="#A3A3A3" size="20" type="search"></uni-icons>
			<view class="" style="margin-left: 10rpx;">
				今日特价
			</view>
		</view>

		<scroll-view class="scrollow" scroll-with-animation scroll-x="true" :scroll-into-view="category.indexid_z">
			<view class="item" @click="changeCategory('1',item)" v-for="(item,index) in category.categoryList"
				:key="index" :id="'z'+item.id">
				<image :class="category.indexid_z == 'z'+item.id?'imgact':''" :src="item.icon" mode=""></image>
				<view class="title" :style="{color:category.indexid_z == 'z'+item.id?'#FF7700':''}">
					{{item.name}}
				</view>
			</view>
		</scroll-view>
		<view class="content">
			<!-- 二级 -->
			<scroll-view class="left" scroll-y="true">
				<view class="left_item" @click="changeCategory('2',item)"
					:class="item.id == category.indexid?'select':''" v-for="(item,index) in category.categoryList1"
					:key="index">
					<view class="x"></view>{{item.name}}
				</view>
			</scroll-view>

			<!-- 三级 -->
			<view class="right">
				<view class="item_abs" v-if="category3Show" @click="open">
					<view class="item_abs_flexs">
						<view class="item_abs_flex">
							<view class="ctit " :class="category.indexid1 == 0?'ctitselect':''"
								@click.stop="changeCategory('3','')">
								全部
							</view>
							<view class="ctit" :class="category.indexid1 == item.id?'ctitselect':''"
								v-for="(item,index) in category.categoryList2" :key="index"
								@click.stop="changeCategory('3',item)">
								{{item.name}}
							</view>
						</view>
						<view class="up flex_jc_cent" @click.stop="open">
							收起
							<uni-icons color="#979797" style="margin-left: 10rpx;" type="up"></uni-icons>
						</view>
					</view>
				</view>
				<view class="item_select">
					<scroll-view class="item_select_scroll" scroll-x="true" scroll-with-animation
						:scroll-into-view="category.category3_z">
						<view class="ctit " :class="category.indexid1 == 0?'ctitselect':''" id='category3_z0'
							@click="changeCategory('3','')">
							全部
						</view>
						<view class="ctit" :class="category.indexid1 == item.id?'ctitselect':''"
							v-for="(item,index) in category.categoryList2" :key="index" :id="'category3_z'+item.id"
							@click="changeCategory('3',item)">
							{{item.name}}
						</view>
					</scroll-view>
					<view class="item_right flex_jc_cent" @click="open">
						<uni-icons color="#979797" type="down"></uni-icons>
					</view>
				</view>
				<scroll-view class="rightsc" @scrolltolower="scrolltolower" :scroll-y="category3Show">
					<view class="item " v-for="(item,index) in category.productList" :key="index">
						<image lazy-load :src="item.icon" mode="aspectFit"></image>
						<view class="item_r">
							<view class="title">
								{{item.name}}
							</view>
							<view class="item_flex">
								<view class="price">
									<text>￥</text>{{item.price}}<text class="t2">/{{item.unit}}</text>
								</view>
								<countNum v-model.number="item.carCount"></countNum>
							</view>
						</view>
					</view>
					<view class="" style="height: 66rpx;"></view>
					<view class="kong" v-if="category.nolist">
						<image src="/static/dkong.png" mode=""></image>
						<view class="text">
							暂无商品
						</view>
					</view>
				</scroll-view>
			</view>

		</view>


		<uv-tabbar :value="1" class="tab">
		</uv-tabbar>
	</view>
</template>

<script setup>
	import {
		ref,
		nextTick,
		reactive
	} from 'vue';
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getproductcategory,
		getcategory
	} from '@/common/http.api.js';
	const category3Show = ref(false) //右侧筛选是否可以滚动 弹窗出来不滚动
	onShow(() => {
		category.category_id = uni.getStorageSync('category_id')
		loadchildrenList(0, category, 'categoryList', () => {
			console.log(category, '--------------');
			category.indexid_z = category.category_id ? 'z' + category.category_id : 'z0'
			let pid = category.category_id ? category.category_id : category.categoryList[0].id
			loadchildrenList(pid, category, 'categoryList1', () => {
				category.indexid = category.categoryList1[0].id
				loadchildrenList(category.indexid, category, 'categoryList2', () => {
					category.indexid = category.categoryList1[0].id
					let name = category.categoryList.find(item => {
						return item.id == category.category_id
					}).name
					params.category1 = name ? name : category.categoryList[0].name
					params.category2 = category.categoryList1[0].name
					getList()
				})
			})

		})
		// if (uni.getStorageSync('category_id')) {
		// 	getcategory({
		// 		parentId: 0
		// 	}).then(res => {
		// 		category.categoryList = res.data
		// 		category.indexid_z = 'z' + uni.getStorageSync('category_id')
		// 		getcategory({
		// 			parentId: uni.getStorageSync('category_id')
		// 		}).then(cat => {
		// 			category.categoryList1 = cat.data
		// 			category.indexid = cat.data[0].id
		// 			getcategory({
		// 				parentId: cat.data[0].id
		// 			}).then(cats => {
		// 				category.categoryList2 = cats.data
		// 				// params.category1 = category.categoryList
		// 				params.category1 = category.categoryList.find(item => {
		// 					return item.id == uni.getStorageSync('category_id')
		// 				}).name
		// 				params.category2 = cat.data[0].name
		// 				getList()
		// 			})
		// 		})
		// 	})
		// } else {
		// 	getCategory(0)
		// }
	})
	const category = reactive({
		category_id:'',
		indexid_z: '',
		category3_z: 'category3_z0',
		indexid: 0,
		indexid1: 0,
		categoryList: [],
		categoryList1: [],
		categoryList2: [],
		productList: [],
		nolist: false
	})
	//
	function loadchildrenList(parentId, category, name, fun) {
		getcategory({
			parentId
		}).then(res => {
			if (res.code == 200) {
				category[name] = res.data
				if (fun && typeof fun === 'function') {
					fun()
				}
			}
		})
	}






	//初始化数据
	function getCategory(id) {
		getcategory({
			parentId: id
		}).then(res => {
			category.categoryList = res.data
			category.indexid_z = 'z' + res.data[0].id
			getcategory({
				parentId: res.data[0].id
			}).then(cat => {
				category.categoryList1 = cat.data
				category.indexid = cat.data[0].id
				getcategory({
					parentId: cat.data[0].id
				}).then(cats => {
					category.categoryList2 = cats.data
					params.category1 = res.data[0].name
					params.category2 = cat.data[0].name
					getList()
				})
			})
		})
	}
	const params = reactive({
		category1: '',
		category2: '',
		category3: '',
		pageNum: 1,
		pageSize: 10,
	})

	function getList() {
		getproductcategory(params).then(res => {
			if (res.code == 200) {
				category.productList = res.rows
				let total = Math.ceil(res.total / params.pageSize);
				category.nolist = total == 0
			}
		})
	}

	//切换分类3 列表重新加载
	function changeCategory(type, item) {
		category3Show.value = false
		params['category' + type] = item ? item.name : ''
		//1级分类点击
		if (type == 1) {
			category.indexid_z = 'z' + item.id
			getcategory({
				parentId: item.id
			}).then(cat => {
				category.categoryList1 = cat.data
				category.indexid = cat.data[0].id
				getcategory({
					parentId: cat.data[0].id
				}).then(cats => {
					category.categoryList2 = cats.data
					category.indexid1 = 0
					params.category2 = cat.data[0].name
					params.category3 = ''
					getList()
				})
			})
		}
		//2级分类点击
		if (type == 2) {
			category.indexid = item.id
			getcategory({
				parentId: item.id
			}).then(cats => {
				category.categoryList2 = cats.data
				category.indexid1 = 0
				params.category3 = ''
				getList()
			})
		}
		//3级分类点击
		if (type == 3) {
			category.indexid1 = item ? item.id : 0
			category.category3_z = item ? 'category3_z' + item.id : 'category3_z0'
			category.productList = []
			params.pageNum = 1
			console.log(category.category3_z);
			getList()
		}
	}

	//上拉加载
	function scrolltolower() {
		params.pageNum++
		getproductcategory(params).then(res => {
			if (res.code == 200) {
				category.productList.push(...res.rows)
				let total = Math.ceil(res.total / params.pageSize);
				category.nolist = total == 0
			}
		})
	}

	function open() {
		category3Show.value = !category3Show.value
	}
</script>

<style lang="scss">
	page {
		background: #F8F8F8;
	}

	.search_flex {
		margin: 20rpx auto;
		display: flex;
		align-items: center;
		width: 710rpx;
		height: 72rpx;
		background: #FFFFFF;
		border-radius: 112rpx 112rpx 112rpx 112rpx;
		padding: 20rpx;
		box-sizing: border-box;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #A3A3A3;
	}

	.scrollow {
		height: 150rpx;
		white-space: nowrap;
		width: 710rpx;
		margin: 0rpx auto;

		.item {
			display: inline-block;
			width: 120rpx;
			height: 100rpx;
			text-align: center;
			margin-right: 17rpx;

			image {
				width: 80rpx;
				height: 80rpx;
				border-radius: 40rpx;
				border: 1px solid #F8F8F8;
			}

			.imgact {
				border: 1px solid #FF7700;
			}

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #7E7E7E;
				margin-top: 10rpx;
			}
		}
	}

	.content {
		height: calc(100vh - 440rpx);
		display: flex;
		justify-content: space-between;

		.left {
			width: 200rpx;
			height: calc(100vh - 320rpx);
			background: #F8F8F8;
			box-sizing: border-box;

			.left_item {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #979797;
				text-align: center;
				height: 90rpx;
				line-height: 90rpx;
				position: relative;

			}

			.select {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #FF7700;
				background: #fff;

				.x {
					position: absolute;
					width: 6rpx;
					height: 30rpx;
					background: #FF7700;
					top: 30rpx;
				}
			}
		}

		.right {
			background: #fff;
			flex: 1;
			position: relative;
			height: calc(100vh - 420rpx);

			.rightsc {
				height: calc(100vh - 520rpx);

				.kong {
					image {
						width: 120rpx;
						height: 120rpx;
					}

					.text {}
				}
			}

			.item_abs {
				position: absolute;
				top: 0%;
				height: 100%;
				width: 100%;
				background: rgba(0, 0, 0, 0.5);
				z-index: 5;

				.item_abs_flexs {
					background: #fff;

					.up {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #979797;
						padding-bottom: 20rpx;
					}
				}

				.item_abs_flex {
					background: #fff;
					display: flex;
					flex-wrap: wrap;
					padding: 20rpx 0rpx 0rpx 20rpx;
					box-sizing: border-box;

					.ctit {
						line-height: 56rpx;
						height: 56rpx;
						background: #F5F5F5;
						border-radius: 38rpx;
						padding: 0px 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #979797;
						margin-right: 20rpx;
						margin-bottom: 20rpx;
					}

					.ctitselect {
						background: #FFF1E5 !important;
						color: #FF7700 !important;
					}

				}
			}

			.item_select {
				height: 90rpx;
				position: sticky;
				top: 0%;
				background: #fff;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.item_right {
					flex: 1;
				}

				.item_select_scroll {
					width: 480rpx;
					height: 90rpx;
					white-space: nowrap;
					line-height: 90rpx;
					margin-left: 20rpx;

					.ctit {
						display: inline-block;
						line-height: 56rpx;
						height: 56rpx;
						background: #F5F5F5;
						border-radius: 40rpx 40rpx 40rpx 40rpx;
						padding: 0px 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #979797;
						margin-right: 20rpx;
					}

					.ctitselect {
						background: #FFF1E5 !important;
						color: #FF7700 !important;
					}
				}
			}

			.item {
				background: #FFFFFF;
				padding: 20rpx 0rpx;
				box-sizing: border-box;
				display: flex;
				margin: 0rpx 20rpx;
				border-bottom: 1px solid #efefef;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 20rpx;
					border: 1px solid #efefef;
				}

				.item_r {
					margin-left: 20rpx;
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #000000;
					}

					.item_flex {
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}
			}

			.item:last-child {
				border: none;
			}
		}
	}
</style>